/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.siscoh.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import org.siscoh.model.bean.AbstractBean;
import org.siscoh.model.bean.CategoriaBean;
import org.siscoh.model.bean.EspecificacaoBean;

/**
 *
 * @author Israel
 */
public class CategoriaDao extends AbstractDao {
    
    private boolean lazy;
    
    public CategoriaDao()
    {
        this.lazy = true;
    }
    
    public CategoriaDao(boolean isLazy)
    {
        this.lazy = isLazy;
    }

    @Override
    public void update(AbstractBean bean) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void insert(AbstractBean bean) {
        CategoriaBean categ = (CategoriaBean) bean;
        
        long id = Conexao.getInstance().insert("categoria", new String[]{"nome"},
                new String[]{categ.getNome()});
        
        categ.setId(id);
        
        if(id != -1)
        {
            EspecificacaoDao ed = new EspecificacaoDao();
            for(EspecificacaoBean eb : categ.getEspecificao())
            {
                eb.setCategoria(categ);
                ed.insert(eb);
            }
            bean.setId(id);
        }
    }

    @Override
    public void delete(AbstractBean bean) {
        
    }

    @Override
    public ArrayList<CategoriaBean> fetchAllArrayList() {        
        ResultSet rs = Conexao.getInstance().select("*", "categoria",null,"ORDER BY nome ASC");
        
        return parseRsToObjects(rs);
    }

    @Override
    public AbstractBean[] fetchAll() {
        
        ArrayList<CategoriaBean> cbList = fetchAllArrayList();
        
        CategoriaBean[] categorias = new CategoriaBean[cbList.size()];
        
        for(int i = 0; i < cbList.size(); i++)
        {
            categorias[i] = cbList.get(i);
        }
        
        return categorias;
    }

    @Override
    public AbstractBean selectById(long id) {
        
        ResultSet rs = Conexao.getInstance().select(null, "categoria", "id = '"+id+"'");
        
        return parseRsToObject(rs);
    }

    @Override
    protected AbstractBean parseRsToObject(ResultSet rs) {
        CategoriaBean cb = new CategoriaBean();
        
        try {
            if(rs.next())
            {
                cb.setId(rs.getLong("id"));
                cb.setNome(rs.getString("nome"));
                
                if(!lazy)
                {
                    ArrayList<EspecificacaoBean> espList = new EspecificacaoDao().selectByCategoria(cb.getId());
                    cb.setEspecificacao(espList);
                }
                
            }
        } catch (SQLException ex) {
            ex.printStackTrace(System.err);
            cb = null;
        }
        
        return cb;
    }

    @Override
    protected ArrayList<CategoriaBean> parseRsToObjects(ResultSet rs) {
        try {
            ArrayList<CategoriaBean> categorias = new ArrayList<CategoriaBean>();
            
            while(rs.next())
            {
                CategoriaBean cb = new CategoriaBean();
                cb.setNome(rs.getString("nome"));
                cb.setId(rs.getLong("id"));
                
                categorias.add(cb);
            }
            
            if(!lazy)
            {
                EspecificacaoDao espDao = new EspecificacaoDao();
            
                for(CategoriaBean cb:categorias)
                {
                    ArrayList<EspecificacaoBean> espList = espDao.selectByCategoria(cb.getId());
                    for(EspecificacaoBean eb:espList)
                    {
                        eb.setCategoria(cb);
                    }
                    cb.setEspecificacao(espList);
                }
            }
            
            return categorias;
        } catch (SQLException ex) {
            System.err.append(ex.getMessage());
            return null;
        }
    }
}